package com.ctgu.tengyun.controller.faultsolution;

import com.ctgu.tengyun.bean.convert.FaultSolutionConvert;
import com.ctgu.tengyun.bean.convert.FileConvert;
import com.ctgu.tengyun.bean.request.ProductFaultSolutionRequest;
import com.ctgu.tengyun.bean.response.FaultSolutionListResponse;
import com.ctgu.tengyun.bean.response.ProductFaultSolutionInfo;
import com.ctgu.tengyun.domain.FaultSolution;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.service.faultsolution.FaultSolutionService;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by liaoweizhi on 2019/8/17.
 */
@RestController
@RequestMapping(value = "/v1/fault_solution/")
public class FaultSolutionController {

    @Resource
    private FaultSolutionService faultSolutionService;
    @Resource
    private ProductInfoService productInfoService;

    @PostMapping(value = "product/save_or_update")
    public Object saveOrUpdate(@RequestBody ProductFaultSolutionRequest request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "refId: productNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getTroubleType()), "troubleType is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getTroubleInfo()), "troubleInfo is required.");
        FileConvert.splitFileBeanUrlPath(request.getVideoFile());

        String productNo = request.getProductNo();
        ProductInfo productInfo = productInfoService.selectByProductNo(productNo);
        Validator.assertResourceFound(null != productInfo, "product not found, productNo:" + productNo);

        FaultSolution faultSolution = new FaultSolution();
        faultSolution.setRefId(productNo);
        faultSolution.setRefType(1);
        BeanUtils.copyProperties(request, faultSolution);

        faultSolutionService.upsert(faultSolution);

        return ImmutableMap.of("id", faultSolution.getId());
    }

    @RequestMapping(value = "product/list")
    public FaultSolutionListResponse list(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String productNo = ParameterTool.getParameterString(request, "productNo", null);
        String keyWords = ParameterTool.getParameterString(request, "keyWords", null);

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        Integer totalCount = faultSolutionService.countFaultSolution(productNo, 1, keyWords);
        List<FaultSolution> faultSolutionList = Collections.emptyList();
        if (totalCount > 0) {
            faultSolutionList = faultSolutionService.selectFaultSolutionList(productNo, 1, keyWords, pageNo, pageSize);
        }

        Set<String> productNoSet = CollectionsUtil.getComposerIdSet(faultSolutionList, FaultSolution.REF_ID_COMPOSER);
        List<ProductInfo> productInfoList = productInfoService.selectByProductNos(productNoSet);
        Map<String, ProductInfo> productNoMap = CollectionsUtil.mapComposerId(productInfoList, ProductInfo.PRODUCT_NO_COMPOSER);

        return FaultSolutionConvert.toFaultSolutionListResponse(totalCount, pageSize, faultSolutionList, productNoMap);
    }

    @GetMapping(value = "/product/detail/{id}")
    public ProductFaultSolutionInfo detail(@PathVariable("id") Long faultSolutionId) {
        FaultSolution faultSolution = faultSolutionService.selectByPrimaryKey(faultSolutionId);
        Validator.assertResourceFound(null != faultSolution, "faultSolution not found, id:" + faultSolutionId);
        ProductInfo productInfo = productInfoService.selectByProductNo(faultSolution.getRefId());

        return FaultSolutionConvert.toProductFaultSolutionInfo(faultSolution, productInfo);
    }

}
